package com.each_time_data.service.impl;

import cn.hutool.core.date.DateUtil;
import com.constant.DateFormatConstant;
import com.each_time_data.config.HttpUtils;
import com.entity.EachDayId;
import com.service.EachDayIdService;
import org.jsoup.nodes.Document;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.BoundHashOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import javax.annotation.Resource;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.concurrent.ThreadLocalRandom;
import java.util.concurrent.TimeUnit;

/**
 * @author Administrator
 */
@Service
public class GetDataServiceImpl {

    @Resource
    private RedisTemplate redisTemplate;
    @Resource
    private EachDayIdService eachDayIdService;

    @Scheduled(fixedRate = 1* 60 * 60000)
    public void findIds() {
        String[] allIdList = new String[]{};
        try {
            String data = UrlService.getDataURL();
            String[] ids = data.split(",");

            allIdList = ids;

        } catch (IOException e) {
            e.printStackTrace();
        }
        for (String id : allIdList) {
            EachDayId game = new EachDayId();
            game.setGameId(id);
            int i = ThreadLocalRandom.current().nextInt(5,10);
            try {
                String gameId = id.trim();
                synchronized (gameId+"") {
                    BoundHashOperations operations = redisTemplate.boundHashOps("waiting");
                    Boolean aBoolean = operations.hasKey(gameId+"");
                    if (aBoolean) {
                        continue;
                    }
                    Thread.sleep(i * 1000);

                    getCrownId(gameId + "");
                    eachDayIdService.insert(game);
                }

            } catch (Exception e) {

            }
        }
        try {
            System.out.println("=================比赛数据获取完成==========="+ DateUtil.format(new Date(),"yyyy年MM月dd日 HH:mm"));
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public synchronized void getCrownId(String ID) throws Exception {

        //获取链接id字符串
        String url = "http://vip.win007.com/changeDetail/3in1Odds.aspx?id=" + ID + "&companyid=3&l=0";
        // String url = "http://vip.win007.com/changeDetail/3in1Odds.aspx?id=1817283&companyid=3&l=0";
        System.out.println("刷选进行中的比赛ID：" + ID);
        Document document = HttpUtils.getHtmlPageResponseAsDocument(url);
        String st = document.getElementsByClass("hg").text();
        String title = null;
        String times = null;
        try {
            String[] split = st.split("比分");
            String substring = split[0];
            times = substring.substring(substring.length() - 26, substring.length() - 2);
            title = substring.substring(0, substring.length() - 26);
        } catch (Exception e) {
            return;
        }
        if (!times.contains("月")) {
            return;
        }
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy 年 MM 月 dd 日 HH:mm");
        Date fistDate = dateFormat.parse(times);
        long gameTime = fistDate.getTime() + 78 * 60 * 1000;
        long nowTime = System.currentTimeMillis();
        System.out.println(ID + "  开赛时间  " + times);
        int tt = (int) (nowTime - fistDate.getTime()) / 60000;
        System.out.println("当前系统时间 - 比赛时间=  " + tt + "分钟");

        BoundHashOperations wiatingId = redisTemplate.boundHashOps("waiting");
        wiatingId.put(ID, gameTime);
        wiatingId.expire(24, TimeUnit.HOURS);

    }

}

